/* License Notice:
**
** This program is free software: you can redistribute it and/or modify
**    it under the terms of the GNU General Public License as published by
**    the Free Software Foundation, either version 3 of the License, or
**    (at your option) any later version.
** This program is distributed in the hope that it will be useful,
**   but WITHOUT ANY WARRANTY; without even the implied warranty of
**   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
**   GNU General Public License for more details.
** You should have received a copy of the GNU General Public License
**   along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/**
 * @file time_keep.c
 * @author TooOld2Rock'nRoll
 * @see time_keep.h
 */
/*---- Includes ----*/
#include "toolbox/time_keep.h"


/*---- Global Variables ----*/


/*---- Function Prototypes ----*/


/*---- Functions Implementation ----*/
double getHiResTime ()
{
    return SDL_GetPerformanceCounter() / (double)SDL_GetPerformanceFrequency();
}//END getHiResTime ()


void sleep (double time)
{
    struct timespec sleep_req;

    sleep_req.tv_sec = (time_t)time;
    sleep_req.tv_nsec = (time - sleep_req.tv_sec) * 1000000000;
    clock_nanosleep (CLOCK_MONOTONIC, 0, &sleep_req, NULL);
}//end sleep ()


double getDeltaTime (time_keep_s *keep, bool reset)
{
    if (!keep)
        return 0.f;

    keep->ui64_current_t = SDL_GetPerformanceCounter ();
    keep->d_delta_t = (keep->ui64_current_t - keep->ui64_last_t)/(double)SDL_GetPerformanceFrequency();

    if (reset)
        keep->ui64_last_t = keep->ui64_current_t;

    return keep->d_delta_t;
}//End getDeltaTime ()

